package me.w70.bot.util;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import me.w70.bot.gui.MainWindow;

public class Output {

	public JScrollPane scroll;

	JEditorPane pane = new JEditorPane();

	String log = "";

	int lines = 0;

	public Output() {
		pane.setPreferredSize(new Dimension(MainWindow.RS_WIDTH, 80));
		pane.setEditable(false);
		pane.setAutoscrolls(true);
		pane.setContentType("text/html");
		scroll = new JScrollPane(pane);
		scroll.setPreferredSize(new Dimension(MainWindow.RS_WIDTH, 80));
		scroll.setAutoscrolls(true);
		scroll
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	}

	public void addMessage(Class from, Object message, boolean warning) {
		lines++;
		String formatted = "";
		String classname = from.getCanonicalName();
		String[] parts = classname.split("\\.");
		/*
		 * try { classname = parts[parts.length - 2] + "." + parts[parts.length -
		 * 1]; } catch (ArrayIndexOutOfBoundsException e) {
		 */classname = parts[parts.length - 1];
		// }
		 
		 if(warning) {
			 formatted += "<div style=\"display:block;width:"+MainWindow.RS_WIDTH+"px;\">";
			 message = "<span style=\"color:#ff0000;\">" + message + "</span>";
		 } else {
			 formatted += "<div style=\"display:block;width:"+MainWindow.RS_WIDTH+"px;\">";
					 
		 }
		formatted += "<b>[" + classname + "]</b> " + message + "";
			formatted += "</div>";
		
		if (lines != 1) {
			//log += "<br/>";
		}
		log += formatted;
		pane.setText("<font face=\"Verdana\" size=\"3\">" +log + "</font>");
		pane.scrollRectToVisible(new Rectangle(0, lines * 30, 1, 1));
	}

}
